package com.ky.manage.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import com.ky.manage.R;

/**
 * 带查看密码的控件
 *
 * @author zhaoyan
 * @date 2020-09-04
 */
public class EditTextWithPwdView extends LinearLayout {
    private EditText pwdEt;
    private ImageView pwdIv;
    private boolean hidePwd = true;
    private Context mContext;

    public EditTextWithPwdView(Context context) {
        this(context, null);
    }

    public EditTextWithPwdView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EditTextWithPwdView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public EditTextWithPwdView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context, attrs);
    }

    /**
     * 初始化view
     *
     * @param context
     * @param attrs
     */
    private void initView(Context context, AttributeSet attrs) {
        mContext = context;
        String editText = "", hintText = "请输入密码";
        int textColor = Color.parseColor("#333333"), hintColor = Color.parseColor("#666666");
        float textSize = 12f;
        int editBgRes = 0;
        if (attrs != null) {
            TypedArray typed = context.obtainStyledAttributes(attrs, R.styleable.EditTextWithPwdView);
            editText = typed.getString(R.styleable.EditTextWithPwdView_editText);
            hintText = typed.getString(R.styleable.EditTextWithPwdView_hintText);
            hidePwd = typed.getBoolean(R.styleable.EditTextWithPwdView_hidePwd, true);
            textColor = typed.getColor(R.styleable.EditTextWithPwdView_textColor, 0);
            hintColor = typed.getColor(R.styleable.EditTextWithPwdView_hintColor, 0);
            textSize = typed.getFloat(R.styleable.EditTextWithPwdView_textSize, 12);
            editBgRes = typed.getResourceId(R.styleable.EditTextWithPwdView_editBg, 0);
            typed.recycle();
        }

        LayoutInflater.from(context).inflate(R.layout.edit_text_pwd_view, this, true);
        pwdEt = findViewById(R.id.et_pwd);
        pwdIv = findViewById(R.id.pwd_iv);
        if (editBgRes > 0) {
            pwdEt.setBackgroundResource(editBgRes);
        }
        if (!TextUtils.isEmpty(editText)) {
            pwdEt.setText(editText);
        }
        if (textColor > 0) {
            pwdEt.setTextColor(textColor);
        }
        if (hintColor > 0) {
            pwdEt.setHintTextColor(hintColor);
        }
        if (textSize > 0) {
            pwdEt.setTextSize(textSize);
        }
        pwdEt.setHint(hintText);

        pwdIv.setOnClickListener(v -> {
            //密码显示隐藏点击事件
            updatePwdState();
        });
        //更新状态
        if (hidePwd) {
            //隐藏密码
            pwdEt.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
            pwdIv.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.hide_pwd, null));
        } else {
            //展示密码
            pwdEt.setInputType(InputType.TYPE_CLASS_TEXT);
            pwdIv.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.show_pwd, null));
        }
    }

    /**
     * 更新密码展示隐藏状态
     */
    private void updatePwdState() {
        if (hidePwd) {
            //展示密码
            hidePwd = false;
            pwdEt.setInputType(InputType.TYPE_CLASS_TEXT);
            pwdIv.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.show_pwd, null));
        } else {
            //隐藏密码
            hidePwd = true;
            pwdEt.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
            pwdIv.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.hide_pwd, null));
        }
        pwdEt.setSelection(pwdEt.getText().toString().length());
    }

    public String getText() {
        if (null != pwdEt) {
            return pwdEt.getText().toString();
        }
        return "";
    }

    public void setText(String editTxt) {
        if (null != pwdEt) {
            pwdEt.setText(TextUtils.isEmpty(editTxt) ? "" : editTxt);
        }
    }
}
